<template>
  <el-dialog
    ref="wkDialog"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    :title="title"
    width="450px"
    @close="handleCancel">
    <span slot="title" class="el-dialog__title">{{ title }}<i class="wk wk-icon-fill-help wk-help-tips" data-type="8" data-id="98" /></span>
    <el-form
      ref="fieldForm"
      :model="form"
      :rules="rules"
      :validate-on-rule-change="false"
      label-position="top">
      <el-form-item
        class="wk-form-item"
        :label="formTitle"
        prop="selectId">
        <el-select v-model="form.selectId" style="width: 100%;">
          <el-option
            v-for="item in list"
            :key="item.poolId"
            :label="item.poolName"
            :value="item.poolId" />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-debounce="handleConfirm" type="primary">
        {{ $t('crm.components.e6bc10d3ede480fbb592df579ef09d57') }}
      </el-button>
      <el-button @click.native="handleCancel">
        {{ $t('crm.components.8018fd010d1fd1cc4e275e60b4685cbe') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  crmCustomerPoolNameListAPI,
  crmCustomerPutInPoolAPI
} from '@/api/crm/customer'
import {
  crmLeadsPoolNameListAPI,
  crmLeadsPutInPoolAPI
} from '@/api/crm/leads'

import ElDialogLoadingMixin from '@/mixins/ElDialogLoading'

export default {
  /**
   * 客户放入公海
   */
  name: 'PutPoolHandle',
  components: {
  },
  mixins: [ElDialogLoadingMixin],
  props: {
    visible: {
      type: Boolean,
      required: true,
      default: false
    },
    // 操作数据
    selectionList: {
      type: Array,
      default: () => {
        return []
      }
    },
    crmType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      form: {
        selectId: ''
      },
      rules: {
        selectId: [{ required: true, message: this.$t('crm.components.6068ce02525d6b3d8ab4184ccf62bb18'), trigger: 'change' }]
      },
      list: []
    }
  },
  computed: {
    title() {
      return {
        leads: '放入线索池',
        customer: this.$t('crm.components.9c6bdba098a7965d2e54e594d79b7b63')
      }[this.crmType]
    },
    formTitle() {
      return {
        leads: '线索池',
        customer: this.$t('crm.components.3a108f639bba37369ec50eb7d711aefc')
      }[this.crmType]
    }
  },
  watch: {
    list: {
      handler() {
        this.form = {
          selectId: this.list && this.list.length > 0 ? this.list[0].poolId : ''
        }
      },
      immediate: true
    },
    visible(val) {
      if (val && this.list.length === 0) {
        this.getList()
      }
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 获取数据源
     */
    getList() {
      this.loading = true
      const request = {
        leads: crmLeadsPoolNameListAPI,
        customer: crmCustomerPoolNameListAPI
      }[this.crmType]
      request()
        .then(res => {
          this.list = res.data || []
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },

    /**
     * 取消选择
     */
    handleCancel() {
      this.$emit('update:visible', false)
    },

    /**
     * 确定
     */
    handleConfirm() {
      this.$refs.fieldForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const request = {
            leads: crmLeadsPutInPoolAPI,
            customer: crmCustomerPutInPoolAPI
          }[this.crmType]
          request({
            ids: this.selectionList.map(item => item[this.crmType + 'Id']),
            poolId: this.form.selectId
          })
            .then(res => {
              this.$message({
                type: 'success',
                message: this.$t('crm.components.f124809a1fc0c57df1aad495656fc7af')
              })
              this.loading = false
              this.$emit('handle', { type: {
                leads: 'put_seas_leads',
                customer: 'put_seas'
              }[this.crmType] })
              this.handleCancel()
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
